package com.example.qt.ptplatform.controller;

import com.example.qt.ptplatform.service.UserFollowService;
import com.example.qt.ptplatform.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/follow")
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    /**
     * 关注用户
     * POST /follow/follow
     */
    @PostMapping("/follow")
    public JsonResult followUser(
            @RequestParam("follower_id") Integer followerId,
            @RequestParam("followed_id") Integer followedId) {
        try {
            if (followerId == null || followedId == null) {
                return JsonResult.error("参数不能为空");
            }

            if (followerId.equals(followedId)) {
                return JsonResult.error("不能关注自己");
            }

            boolean result = userFollowService.followUser(followerId, followedId);
            if (result) {
                Map<String, Object> data = new HashMap<>();
                data.put("follower_id", followerId);
                data.put("followed_id", followedId);
                return JsonResult.success("关注成功", data);
            } else {
                return JsonResult.error("关注失败，可能已关注");
            }
        } catch (Exception e) {
            return JsonResult.error("关注失败: " + e.getMessage());
        }
    }

    /**
     * 取消关注
     * POST /follow/unfollow
     */
    @PostMapping("/unfollow")
    public JsonResult unfollowUser(
            @RequestParam("follower_id") Integer followerId,
            @RequestParam("followed_id") Integer followedId) {
        try {
            if (followerId == null || followedId == null) {
                return JsonResult.error("参数不能为空");
            }

            boolean result = userFollowService.unfollowUser(followerId, followedId);
            if (result) {
                Map<String, Object> data = new HashMap<>();
                data.put("follower_id", followerId);
                data.put("followed_id", followedId);
                return JsonResult.success("取消关注成功", data);
            } else {
                return JsonResult.error("取消关注失败，可能未关注");
            }
        } catch (Exception e) {
            return JsonResult.error("取消关注失败: " + e.getMessage());
        }
    }

    /**
     * 检查是否已关注
     * GET /follow/check?follower_id=1&followed_id=2
     */
    @GetMapping("/check")
    public JsonResult checkFollowStatus(
            @RequestParam("follower_id") Integer followerId,
            @RequestParam("followed_id") Integer followedId) {
        try {
            if (followerId == null || followedId == null) {
                return JsonResult.error("参数不能为空");
            }

            boolean isFollowing = userFollowService.isFollowing(followerId, followedId);
            Map<String, Object> data = new HashMap<>();
            data.put("is_following", isFollowing);
            data.put("follower_id", followerId);
            data.put("followed_id", followedId);
            return JsonResult.success("查询成功", data);
        } catch (Exception e) {
            return JsonResult.error("查询失败: " + e.getMessage());
        }
    }


    /**
     * 获取用户的关注列表（带总数）
     * GET /follow/following?user_id=1
     */
    @GetMapping("/following")
    public JsonResult getFollowingListWithCount(@RequestParam("user_id") Integer userId) {
        try {
            if (userId == null) {
                return JsonResult.error("用户ID不能为空");
            }

            // 获取关注列表
            List<Integer> followingList = userFollowService.getFollowingList(userId);
            // 获取关注总数
            int followingCount = userFollowService.getFollowingCount(userId);

            Map<String, Object> data = new HashMap<>();
            data.put("user_id", userId);
            data.put("following_list", followingList);
            data.put("following_count", followingCount);
            data.put("total", followingCount); // 添加总数字段

            return JsonResult.success("获取关注列表成功", data);
        } catch (Exception e) {
            return JsonResult.error("获取关注列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户的粉丝列表（带总数）
     * GET /follow/followers?user_id=1
     */
    @GetMapping("/followers")
    public JsonResult getFollowerListWithCount(@RequestParam("user_id") Integer userId) {
        try {
            if (userId == null) {
                return JsonResult.error("用户ID不能为空");
            }

            // 获取粉丝列表
            List<Integer> followerList = userFollowService.getFollowerList(userId);
            // 获取粉丝总数
            int followerCount = userFollowService.getFollowerCount(userId);

            Map<String, Object> data = new HashMap<>();
            data.put("user_id", userId);
            data.put("follower_list", followerList);
            data.put("follower_count", followerCount);
            data.put("total", followerCount); // 添加总数字段

            return JsonResult.success("获取粉丝列表成功", data);
        } catch (Exception e) {
            return JsonResult.error("获取粉丝列表失败: " + e.getMessage());
        }
    }
}